package com.jdkreader.concurent.forkjoin;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * 文件夹类
 *
 *  树结构的文件夹类，包含 subFolder子文件夹类和documents文件类
 * @author zhaoyiwu
 * @create 2018-03-12 14:27
 **/
public class Folder {

    private final List<Folder> subFolders;

    private final List<Document> documents;

    public Folder(List<Folder> subFolders, List<Document> documents) {
        this.subFolders = subFolders;
        this.documents = documents;
    }


    public List<Folder> getSubFolders() {
        return subFolders;
    }

    public List<Document> getDocuments() {
        return documents;
    }


    static Folder fromDirectory(File dir) throws IOException {

        List<Folder> subFolder=new LinkedList<Folder>();

        List<Document> documents=new LinkedList<Document>();

        //返回文件夹下的所有文件和目录的绝对路径
        for(File enrty:dir.listFiles()){

            //目录
            if(enrty.isDirectory()){
                subFolder.add(Folder.fromDirectory(enrty));
                //文件
            }else{
                documents.add(Document.fromFile(enrty));
            }
        }

        return new Folder(subFolder,documents);

    }
}
